package com.pro.controller;

import com.google.gson.Gson;
import com.pro.pojo.JsAuditLog;
import com.pro.pojo.JsCourseRecord;
import com.pro.pojo.JsonData;
import com.pro.service.JsAuditLogService;
import com.pro.service.JsCourseRecordService;
import com.pro.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Record")
public class JsCourseRecordController {
    @Autowired
    private JsCourseRecordService service;

    @Autowired
    private JsAuditLogService jsAuditLogService;

    @Autowired
    private RedisCache redisCache;

    private Gson gson = new Gson();

    @RequestMapping("/selectRecord")
    public JsonData selectRecord(String result, Integer productCode,Integer limit, Integer page){
        if(result!=null && result !=""){
            result = "%"+result+"%";
        }else{
            result = "%%";
        }
        List<JsCourseRecord> list = service.selectProduct(result,limit,page);
        Integer count = service.countProduct(result);

        //redis缓存
        String value = redisCache.getCacheObject("RecordSelectProduct");
        List list1 = gson.fromJson(value, List.class);
        Integer integer = redisCache.getCacheObject("RecordCountProduct");
        return list1!=null? JsonData.buildSuccess(list1,integer): JsonData.buildError("服务端错误");
    };

    @RequestMapping("/selectNum")
    public JsonData selectNum(String result){
        result = result!="" ? "%"+result+"%":"%%";
        Integer integer = service.countProduct(result);
        return integer >= 0 ? JsonData.buildSuccess(integer): JsonData.buildError("服务端错误");
    };

    @RequestMapping("/selectThree")
    public JsonData selectThree(String productName, String title, String trueName,Integer limit, Integer page){
        productName = productName!="" ? "%"+productName+"%":"%%";
        title = title!="" ? "%"+title+"%":"%%";
        trueName = trueName!="" ? "%"+trueName+"%":"%%";
        List<JsCourseRecord> list = service.selectThree(productName, title, trueName,limit,page);
        Integer count = service.countThree(productName, title, trueName);
        return list!=null? JsonData.buildSuccess(list,count): JsonData.buildError("服务端错误");
    };

    @RequestMapping("/selectGaoJi")
    public JsonData selectGaoJi(
                                String productCode,
                                String title,
                                String productName,
                                String currentpriceProduct,
                                String trueName,
                                String result,
                                Integer limit,
                                Integer page){
        productCode = productCode!=""?"%"+productCode+"%":"%%";
        title = title !=""?"%"+title+"%":"%%";
        productName = productName !=""?"%"+productName+"%":"%%";
        currentpriceProduct = currentpriceProduct !=""?"%"+currentpriceProduct+"%":"%%";
        trueName = trueName !=""?"%"+trueName+"%":"%%";
        result = result !=""?"%"+result+"%":"%%";

        List<JsCourseRecord> list = service.selectGaoJi(productCode,title,productName,currentpriceProduct,trueName,result,limit,page);
        Integer count = service.countGaoJi(productCode,title,productName,currentpriceProduct,trueName,result);
        return list!=null? JsonData.buildSuccess(list,count): JsonData.buildError("服务端错误");
    };

    @RequestMapping("/insertRecord")
    public JsonData insertRecord(JsCourseRecord jsCourseRecord){
        String result = jsCourseRecord.getResult();
        if(result=="yes"){
            jsCourseRecord.setResult("1");
        }else if(result=="no"){
            jsCourseRecord.setResult("2");
        }
        Boolean aBoolean = service.insertRecord(jsCourseRecord);
        JsAuditLog jsAuditLog = new JsAuditLog();
        jsAuditLog.setType(jsCourseRecord.getResult());
        jsAuditLog.setUserId(jsCourseRecord.getImplementId());
        String implementDate = jsCourseRecord.getImplementDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse =null;
        try{
            parse = sdf.parse(implementDate);
        }catch (Exception e){
            e.printStackTrace();
        }
        jsAuditLog.setCreateDate(parse);
        jsAuditLog.setAduitLogType(1);
        jsAuditLog.setMainId(jsCourseRecord.getId());
        Boolean aBoolean1 = jsAuditLogService.addAuditLog(jsAuditLog);

        return aBoolean!=false? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    };

    @RequestMapping("/addJsCourseRecord")
    public JsonData addJsCourseRecord(JsCourseRecord jsCourseRecord){
        jsCourseRecord.setApplyDate(new Date());
        Boolean aBoolean = service.addJsCourseRecord(jsCourseRecord);
        return aBoolean!=false? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    }

    @RequestMapping("/getDetailed")
    public JsonData getDetailed(Integer productId){
        List<JsCourseRecord> list = service.getDetailed(productId);
        return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务器错误");
    }
    @RequestMapping("/productRecord")
    public JsonData productRecord(){
        int productRecord = service.productRecord();
        return productRecord>=0?JsonData.buildSuccess(productRecord):JsonData.buildError("服务端错误");
    }
}
